<?
session_start();
error_reporting(E_ERROR|E_PARSE);

function _log($msg) {
	$_SESSION['FF']['logger'] .= "*" . $msg . "<br>";
	if(is_file('errorlog.txt'))
		$logFile = 'errorlog.txt';
	else 
		$logFile = '../errorlog.txt';
	$fc = fopen($logFile, 'a') or die("can't open file");
	fwrite($fc, '
'.date("m-d-Y").'> ' . $msg);
	fclose($fc);
}
function _logToPage($msg) {
	_log($msg);
	$_SESSION['FF']['pageLogger'] .= $msg."<br />";
}

function myAddSlashes( $string ) {
	if (get_magic_quotes_gpc()==1) {
		return ( $string );
	} else {
		return ( addslashes ( $string ) );
	}
}

if(is_dir('inc')) $relPath = 'inc/';
else if(is_dir('../inc')) $relPath = '../inc/';
else if(is_dir('../../inc')) $relPath = '../../inc/';

include ($relPath . 'classes.php');
include ($relPath . 'crones.php');

$_CONF = new FFxml($relPath . 'config.xml');
$host = strToUpper($_SERVER['HTTP_HOST']);
$host = str_replace(".", "", $host);

$_DB = new DBmanager($_CONF->get("CONFIG>DATABASE>".$host.">USER.nodeValue"), $_CONF->get("CONFIG>DATABASE>".$host.">PWD.nodeValue"), $_CONF->get("CONFIG>DATABASE>".$host.">HOST.nodeValue"), $_CONF->get("CONFIG>DATABASE>".$host.">NAME.nodeValue"));

// Includo il pacchetto per la lingua selezionata
include ($relPath . 'dictionary/'.$_CONF->get('CONFIG>ADMIN>ADMINLANGUAGE.nodeValue'));

/*********************/
/* LOGIN LOGIN LOGIN */
/*********************/
switch($_GET['action']) {
	
	case 'login': 
		if(md5($_POST['pwd']) == $_CONF->get("CONFIG>ADMIN>ADMINPWD.nodeValue")) {
			// Sono amministratore supremo - GOD
			$_SESSION['FF']['logged'] = true;
			$_SESSION['FF']['logType'] = 2;		//0 => user, 1 => admin user, 2 => god
			$_SESSION['FF']['logName'] = 'Administrator';
		} else {
			$users = $_CONF->get("CONFIG>USERS.childNodes");
			for($i=0; $i<count($users); $i++) {
				if($_POST['pwd'] == $_CONF->get("ACCOUNT.pwd", $users[$i])) {
					$_SESSION['FF']['logged'] = true;
					$_SESSION['FF']['logType'] = (($_CONF->get("ACCOUNT.type", $users[$i]) == 0)?0:1);		//0 => user, 1 => admin user, 2 => god
					$_SESSION['FF']['logName'] = $_CONF->get("ACCOUNT.name", $users[$i]);
					$_SESSION['FF']['logArray'] = $_CONF->get("ACCOUNT.node", $users[$i]);
				}
			}
		}
		if(!$_SESSION['FF']['logged']) echo $_MSG['login_error_pwd'];
		else {
			_log("LOG: ".$_SESSION['FF']['logName']." loggato correttamente (".$_SESSION['FF']['logType'].")");
			?><script>window.location = 'index.php';</script><?
		}
		break;
		
	case 'logout':
		$_SESSION['FF'] = null;
		_log("LOGOUT!");
		?>
		<!-- Non sono loggato -->
		<script>window.parent.location = 'login.php';</script>
		<?
		break;
	
}

if(!$_SESSION['FF']['logged'] && !$_GET['action']) { 

	// devo mandare a index.php
	?>
	<script>window.location = 'login.php?action=mustLogin';</script>
	<?

}
?>


